package com.internetCafes.spms.common.AIRecognize.handler.impl;

import com.internetCafes.spms.common.AIRecognize.handler.WordHandler;
import com.internetCafes.spms.web.certificate.vo.AIRecognizeInfo;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LinYe
 * @date 2020/12/15
 **/
@Component
public class HonorWordHandler implements WordHandler {
    @Override
    public AIRecognizeInfo handle(String word, String filePath) {
        AIRecognizeInfo aiRecognizeInfo= new AIRecognizeInfo();
        String[] split = word.split("\n");
        String name = handleName(split);
        if (name!=null){
            aiRecognizeInfo.setName(name);
        }
        String code = handleCode(split);
        if (code!=null){
            aiRecognizeInfo.setCode(code);
        }
        Date[] dates = handleDate(word,"\\d{4}年\\d{1,2}月\\d{1,2}日");
        if (dates!=null){
            aiRecognizeInfo.setReleaseDate(dates[0]);
            aiRecognizeInfo.setExpireDate(dates[1]);
        }else {
            dates=handleDate(word,"\\d{4}\\.\\d{1,2}\\.\\d{1,2}");
            if (dates!=null){
                aiRecognizeInfo.setReleaseDate(dates[0]);
                aiRecognizeInfo.setExpireDate(dates[1]);
            }
        }
        return aiRecognizeInfo;
    }

    public String handleName(String[] split) {
        for (String line : split) {
            if (line.contains("AAA") && line.length() >= "诚信AAA企业".length()) {
                return line;
            }
        }
        for (String line : split) {
            if ((line.contains("企业") || line.contains("单位")) && line.length() > 5) {
                return line;
            }
        }
        return null;
    }

    public String handleCode(String[] split) {
        for (String s : split) {
            String line = s;
            if (line.contains("编号")) {
                int prefix = line.indexOf(":");
                if (prefix == -1) {
                    prefix = line.indexOf("：");
                }
                if (prefix != -1) {
                    line = line.substring(prefix + 1);
                }
                return line.replace(" ", "");
            }
        }
        return null;
    }

    public Date[] handleDate(String word,String reg) {
        Date[] result=new Date[2];
        Pattern pattern = Pattern.compile (reg);
        Matcher matcher = pattern.matcher (word);
        if (matcher.find()){
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日" );
            Date firstDate = null;
            Date secondDate = null;
            try {
                firstDate = sdf.parse(matcher.group());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (matcher.find()) {
                try {
                    secondDate = sdf.parse(matcher.group());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (firstDate.before(secondDate)) {
                    result[0] = firstDate;
                    result[1] = secondDate;
                } else {
                    result[1] = firstDate;
                    result[0] = secondDate;
                }
                return result;
            }
        }
        return null;
    }
}
